package com.lswms.common.utils;

import java.util.Random;

/**
 * @author bcahlit
 *
 */
public class OpenDoubleQiu {
    int[] red;
    int blue;
    String name;
 
    /**
     * @param name
     *            生成彩票的所有者 或者 开奖号码，用于在开奖是进行区分
     */
    public OpenDoubleQiu(String reds,String blues,String name) {
    	this.name = name;
    	red = new int[reds.split(" ").length];
        for (int i = 0; i < reds.split(" ").length; i++) {
            red[i] = Integer.parseInt(reds.split(" ")[i].toString());
        }
        blue = Integer.parseInt(blues);
    }
 
    /**
     * @param red
     *            一个数组（含有生产的号码）或者是开奖号码
     * @param temR
     *            进行比较的号码
     * @return 在red中是否含有temR
     * 
     *         在生成号码时用来防止 产生相同的号码，在开奖时用来查询重合的号码
     */
    private static boolean existe(int red[], int temR) {
        // TODO Auto-generated method stub
        int i = 0;
        while (red[i] != 0 && i < red.length) {
            // System.out.println(i);
            if (temR == red[i]) {
                return true;
            }
            if (i >= 5)
                return false;
            i++;
        }
        return false;
    }
 
    /**
     * 展示摇取的号码
     */
    public void shownumber(String red,String blue) {
        if (this.name != "drawLottery") {
            System.out.print(this.name + "的号码：");
        } else
            System.out.print("开奖号码：");
 
        for (int i = 0; i < red.split(" ").length; i++) {
            System.out.print(red.split(" ")[i] + " ");
        }
        System.out.println(blue);
    }
 
    /**
     * @param drawLottery 开奖号码
     * @param e1 摇到的号码
     * 
     * 此函数，根据摇号与开奖号 确定中奖等级
     */
    public static int openAward(OpenDoubleQiu drawLottery, OpenDoubleQiu e1) {

    	int level = 0;
        if (drawLottery.name != "drawLottery") {
            System.out.println("参数不为开奖号码");
            return 0;
        }
        int redover = 0;
        for (int i = 0; i < 6; i++) {
            if (existe(drawLottery.red, e1.red[i])) {
                redover++;
            }
        }
         System.out.println(redover);
        if (drawLottery.blue != e1.blue && (redover == 0 || redover == 1 || redover == 2 || redover == 3)) {
            System.out.println(e1.name + "未中奖");
        } else {
            System.out.print("恭喜"+e1.name+"中了");
            switch (redover) {
                case 0: {
                    if(drawLottery.blue == e1.blue){
                    	level = 6;
                    }
                    break;
                }
                case 1: {
                    if(drawLottery.blue == e1.blue){
                    	level = 6;
                    }
                    break;
                }
                case 2: {
                    if(drawLottery.blue == e1.blue){
                    	level = 6;
                    }
                    break;
                }
                case 3: {
                    if(drawLottery.blue == e1.blue){
                    	level = 5;
                    }
                    break;
                }
                case 4: {
                    if(drawLottery.blue == e1.blue){
                    	level = 4;
                    }else{
                    	level = 5;
                    }
                    break;
                }
                case 5: {
                    if(drawLottery.blue == e1.blue){
                    	level = 3;
                    }else{
                    	level = 4;
                    }
                    break;
                }
                case 6: {
                    if(drawLottery.blue == e1.blue){
                    	level = 1;
                    }else{
                    	level = 2;
                    }
                    break;
                }
            }
        }
        return level;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	OpenDoubleQiu e1=new OpenDoubleQiu("09 14 21 23 26 32","1","userid");
        
        OpenDoubleQiu drawLottery=new OpenDoubleQiu("09 14 21 23 26 32","3","drawLottery");
        
 
       int level = OpenDoubleQiu.openAward(drawLottery,e1);
       System.out.println(level);
    }
}
